package fate.nsa;


import fate.ArchiveEntry;

import java.io.IOException;
import java.io.RandomAccessFile;

public class NSAEntry implements ArchiveEntry {
    private final String path;
    private final String name;
    private final int offset;
    private final int size;
    private final int expandsize;

    public NSAEntry(String path, String name, int offset, int size, int expandsize) {
        this.path = path;
        this.name = name;
        this.offset = offset;
        this.size = size;
        this.expandsize = expandsize;
        if (expandsize != size) {
            throw new RuntimeException("expandsize != size, decompress is not supported yet");
        }
    }

    public String getName() {
        return name;
    }

    public long getSize() {
        return size;
    }

    public byte[] read(RandomAccessFile file) throws IOException {
        byte[] buffer = new byte[size];
        file.seek(offset);
        file.readFully(buffer);
        return buffer;
    }


    public byte[] read() throws IOException {
        RandomAccessFile file = new RandomAccessFile(path, "r");
        try {
            return read(file);
        } finally {
            file.close();
        }
    }
}
